﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.UserControl;
using BestBuy.DAL;
using BestBuy.Common;

namespace BestBuy.PageUser
{
    public partial class AddBuyOffer : System.Web.UI.Page
    {
        cBangTinMua cBangTinMua = new cBangTinMua();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        protected void PostNewListingWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (PostNewListingWizard.ActiveStep == SelectCategoryStep)
            {
                Page.Validate("SelectCategoryValidators");
            }
            if (PostNewListingWizard.ActiveStep == ProductDetailsStep)
            {
                Page.Validate("ProductDetailValidations");
            }

            if (!IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void PostNewListingWizard_ActiveStepChanged(object sender, EventArgs e)
        {
            if (PostNewListingWizard.ActiveStep == SelectCategoryStep)
            {
                var nextButton = PostNewListingWizard.FindControl("StepNextButton") as Button;
                if (nextButton != null)
                {
                    nextButton.ValidationGroup = "SelectCategoryValidators";
                }
            }
            if (PostNewListingWizard.ActiveStep == ProductDetailsStep)
            {
                var nextButton = PostNewListingWizard.FindControl("StepNextButton") as Button;
                if (nextButton != null)
                {
                    nextButton.ValidationGroup = "ProductDetailValidations";
                }
                int? productID = SelectCategoryControl.SelectedSubCategoryID;
                if (productID != null)
                {
                    ProductDetailControl.LoadCategory((int)productID);
                }
            }
            if (PostNewListingWizard.ActiveStep == PreviewStep)
            {
                LoadPreviewProduct(ProductDetailControl, PreviewProductControl);
            }
        }

        protected void SideBar_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            LinkButton button = e.Item.FindControl("sideBarButton") as LinkButton;
            if (button != null)
            {
                if (e.Item.DisplayIndex <= PostNewListingWizard.ActiveStepIndex)
                {
                    button.Enabled = true;
                    button.CssClass = "enabled";
                }
                else
                {
                    button.Enabled = false;
                    button.CssClass = "disabled";
                }

            }
        }

        protected void PostNewListingWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (!IsValid)
            {
                e.Cancel = true;
                return;
            }
            CreateAuction(ProductDetailControl);
            Response.Redirect("~/PageUser/MbBuying.aspx");
        }

        private void CreateAuction(ProductDetail detail)
        {
            BangTinMua item = new BangTinMua();
            DanhMucMua dm = new DanhMucMua();

            dm.IDDanhMuc = detail.CategoryID;
            item.DanhMucMuas.Add(dm);
            item.TieuDe = detail.Title;
            item.TuKhoa = detail.Keyword;
            item.NoiDung = detail.Description;
            DateTime dCreate = DateTime.Today;
            item.NgayDang = dCreate.ToShortDateString();
            dCreate = dCreate.AddDays(detail.ExpiryTime);
            item.ThoiHanDang = dCreate.ToShortDateString();
            item.TanSuatMuaHang = detail.BuyingFrequency;
            item.SoLuongMua = detail.QuantityRequired;
            item.IDDonVi = detail.UnitQuantityRequired;
            item.KhoiLuongMuaHangNam = detail.AnnualPurchaseVolume;
            item.IDDonViHangNam = detail.UnitAnnualPurchaseVolume;
            item.KyHangGiaoHang = detail.ShippingTerms;
            item.CangDen = detail.DestinationPort;
            int[] paymentID = detail.Payment;
            string strPayment = "";
            for (int i = 0; i < paymentID.Count(); i++)
            {
                strPayment += paymentID[i].ToString() + "+";
            }
            item.PhuongThucThanhToan = strPayment;
            item.IDTaiKhoan = int.Parse(Session["IDDN"].ToString());
            item.TinhTrang = (int)BangTinMuaStatus.Unverified;
            
            cBangTinMua.AddBangTinMua(item);
        }

        private void LoadPreviewProduct(ProductDetail detail, ProductPreview preview)
        {
            preview.CategoryID = detail.CategoryID;
            preview.Title = detail.Title;
            preview.Keyword = detail.Keyword;
            preview.Description = detail.Description;
            preview.ExpiryTime = detail.ExpiryTime;
            preview.BuyingFrequency = detail.BuyingFrequencyString;
            preview.QuantityRequired = detail.QuantityRequired;
            preview.UnitQuantityRequired = detail.UnitQuantityRequired;
            preview.AnnualPurchaseVolume = detail.AnnualPurchaseVolume;
            preview.UnitAnnualPurchaseVolume = detail.UnitAnnualPurchaseVolume;
            preview.ShippingTerms = detail.ShippingTerms;
            preview.DestinationPort = detail.DestinationPort;
            preview.Payment = detail.Payment;
        }

    }
}